package sanrenxing.dayEnd.backRepertoryTotal.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sanrenxing.dayEnd.backRepertoryTotal.dao.TransactionDao;
import sanrenxing.dayEnd.backRepertoryTotal.domain.Depositbusiness;
import sanrenxing.dayEnd.backRepertoryTotal.service.DepositbusinessService;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @desrciption 业务存款 核心类 控制器类
 * @auth：yidu 黄雄珠
 * @date:2021/3/13 0013 - 9:48
 * @version:1:0
 */
@RestController//风格
@RequestMapping("/controller")//进入核心界面名字
public class DepositbusinessController {
    //自动装配
    @Autowired
    private DepositbusinessService depositbusinessService;
    //dao对象
    @Autowired
    private TransactionDao transactionDao;

    /**
     * 分页查询
     * @param req 请求对象
     * @param page 页数
     * @param limit 行数
     * @return 返回map集合从后端填充数据到前端
     */
    @GetMapping("/depositbusiness")
    public Map<String,Object> selectDesit(HttpServletRequest req, int page, int limit){
        //deposit对象
        Depositbusiness deposit = new Depositbusiness();
        //接收来自请求中的业务参数businType
        String businType = req.getParameter("businType");
        //判断businType是否为空且不等于零
        if(businType!=null && businType.trim().length()!=0){
            //将传递的业务类型保存到业务类型
            deposit.setBusinType(Integer.parseInt(businType));
        }
        //接收来自请求中的到期时间expiryDate
        String expiryDate = req.getParameter("expiryDate");
        //判断expiryDate是否为空 注意是：日期类型
        if(expiryDate!=null && expiryDate.trim().length()!=0){
            //创建一个日期转化类型SimpleDateFormat
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            try {
                //将转化的日期保存到expiryDate
                deposit.setExpiryDate(sdf.parse(expiryDate));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        //根据条件调用相应的方法getcondition  参数deposit
        String getcondition = depositbusinessService.getcondition(deposit);
        //执行多条件查询分页
        List<Depositbusiness> depositList = depositbusinessService.findByPage(limit, page, getcondition);
        //根据条件统计出行数
        int count = depositbusinessService.count(getcondition);
        //创建一个map向前端发送数据
        Map<String,Object> map=new HashMap<>();
        //在映射集合中添加key为code，值为0的状态码
        map.put("code",0);
        //在映射集合中添加key为msg,值为响应信息字符串
        map.put("msg","");
        //在映射集合中添加key为count,值总行数的数据对
        map.put("count",count);
        //在映射集合中添加key为data,值为员集合对象
        map.put("data",depositList);
        //在映射集合中添加key为fund,值为.findFund()
        map.put("fund",transactionDao.findFund());
        //在映射集合中添加key为cashAccount,值为.queryCash()
        map.put("cashAccount",transactionDao.queryCash());
        return map;
    }
    @PostMapping("/depositbusiness")
    /**
     * 新增方法
     * @param depositbusiness 对象
     * @return 0,failure 1，SUCCESS
     */
    public String add(@RequestBody Depositbusiness depositbusiness){
        //将添加数据影响的行数添加到rows
        int rows=depositbusinessService.add(depositbusiness);
        //判断返回值是否为0
        return rows>0?"add SUCCESS":"add failure";
    }
    /**
     * 删除方法
     * @param deposiId 证券编号
     * @return 0,failure 1，SUCCESS
     */
    @DeleteMapping("/depositbusiness/{deposiId}")
    public String delete(@PathVariable("deposiId") int deposiId){
        //将删除数据影响的行数添加到rows
        int rows=depositbusinessService.delete(deposiId);
        //判断返回值是否为0
        return rows>0?"delete SUCCESS":"delete failure";
    }
    /**
     * 修改方法方法
     * @param depositbusiness 对象
     * @return 0,failure 1，SUCCESS
     */
    @PutMapping("/depositbusiness")
    public String update(@RequestBody Depositbusiness depositbusiness){
        //将修改数据影响的行数添加到rows
        int rows=depositbusinessService.update(depositbusiness);
        //判断返回值是否为0
        return rows>0?"update SUCCESS":"update failure";
    }
    /**
     * 批量删除
     * @param deposiIdStr 数据证券ID
     * @return 0,failure 1，SUCCESS
     */
    @PutMapping("/deposi/{deposiIdStr}")
    public String batchBeleteById(@PathVariable("deposiIdStr") String deposiIdStr){
        //获取前端传递编号进行切割
        String[] split = deposiIdStr.split(",");
        //定义返回值"delete success"
        String result = "delete success";
        //循环遍历进行批量删除
        for (String s : split) {
            int delete = depositbusinessService.delete(Integer.parseInt(s));
            if(delete<=0){
                return "batch delete failure";
            }
        }
        return result;
    }

    /**
     * 存款业务点击结算时的业务处理
     * @param str
     * @return
     */
    @PutMapping("/finish/{str}")
    public String finishCash(@PathVariable("str")String str){
        return depositbusinessService.addCash(str)?"success":"fail";
    }

    /**
     * 活期取款
     * @param depositbusiness
     * @return
     */
    @DeleteMapping("/method/delete")
    public String getMoney(@RequestBody Depositbusiness depositbusiness){
        //调用方法，实现取款
        return depositbusinessService.getMoney(depositbusiness)?"success":"fail";
    }
}
